
<template>
  <div class="execution">
    <el-dialog
      :title="mode === 'import' ? '导入数据' : mode === 'verify' ? '确认数据' : '预览数据'"
      :visible="!!mode || !!previewTable"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :destroy-on-close="true"
      width="80%"
    >
      <ImportData
        v-if="mode === 'import'"
        @uploadSuccess="mode = 'verify'"
        @close="handleClosedDialog"
      />
      <Verify
        v-else-if="mode === 'verify'"
        @done="handleClosedDialog"
        @close="handleClosedDialog"
      />
      <Preview
        v-else-if="previewTable !== null"
        @close="previewTable = null"
        :target="previewTable"
      />
    </el-dialog>
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
      >
        <template slot="menuRight">
          <el-button
            v-if="permissionList.import"
            type="primary"
            size="small"
            @click="handleImport"
          >分车站数据导入</el-button>
           <el-button
            type="primary"
            size="small"
            @click="createInventoryComparison"
          >生成营销车票与车站库存对比</el-button>
        </template>
        <template
          slot-scope="scope"
          slot="menu"
        >
          <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="handleView(scope.row, scope.index)"
          >预览</el-button>
        </template>
      </avue-crud>
      <open-dialog
        :DialogVisible.sync="editVisableDialog"
        title="生成营销车票与车站库存对比"
        width="40%"
      >
        <el-form label-width="60px" v-if="editVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="createFareOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-income">
            <el-button type="primary" :loading="loading" @click="editFare()">生成</el-button>
            <el-button type="primary" @click="editVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, tables,createFares } from '@/api/rca/swckbomsmallpayment'
import { tableOption,startDate, endDate } from '@/const/crud/rca/swckbomsmallpayment'
import { mapGetters } from 'vuex'
import { dateFormat } from '@/util/date';
import ImportData from "./components/ImportData";
import Verify from "./components/Verify";
import Preview from "./components/Preview";

export default {
  name: 'swckbomsmallpayment',
  components: {
    ImportData,
    Verify,
    Preview,
  },
  data () {
    return {
      searchForm: {
        tradeDate: [startDate, endDate]
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      loading: false,
      tableOption: tableOption,

      mode: null,
      previewTable: null,
      formCreate: {},
      editVisableDialog: false,
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        import: this.vaildData(this.permissions.sw_ck_bom_small_payment_import, false),
      };
    },
    createFareOption(){
      return {
        emptyBtn: false,
        submitBtn: false,
        column: [ {
          "type": "date",
          "label": "开始日期",
          "prop": "startDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择开始日期',
              trigger: 'blur'
            }
          ]
        }, {
          "type": "date",
          "label": "结束日期",
          "prop": "endDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择结束日期',
              trigger: 'blur'
            }
          ]
        },{
          label: '线路',
          type: "select",
          prop: 'lineCodes',
          props: {
            label: 'name',
            value: 'code'
          },
          multiple: false,//多选控制
          dicUrl: '/support/swparamline/dict'
        }]
      }
    }
  },
  methods: {
    async getList (page, params) {
      this.tableLoading = true;
      try {
        const [startDate, endDate] = this.searchForm.tradeDate || [];
        const response = await fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize,
            },
            params,
            this.searchForm,
            {
              startDate,
              endDate,
            }
          )
        );
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      } catch (e) {
        this.tableLoading = false;
      }
    },
    async searchChange (form, done) {
      const { importTime } = form;
      if (importTime) {
        const [startDate, endDate] = importTime;
        Object.assign(form, {
          startDate: dateFormat(startDate, "yyyy-MM-dd"),
          endDate: dateFormat(endDate, "yyyy-MM-dd"),
        });
      }
      this.searchForm = form;
      try {
        await this.getList(this.page, form);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page);
    },

    handleView (row, index) {
      this.previewTable = row
    },
    handleImport () {
      this.mode = 'import'
    },
    searchReset () {

      this.page.currentPage = 1
      this.getList(this.page);
    },
    handleClosedDialog () {
      this.mode = null
      this.refreshChange()
    },
    async createInventoryComparison(){
      let res = await this.$confirm('操作后原来已确认的数据需要再次重新确认？', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning'
      });
      if (!res) {
        return;
      }
      this.editVisableDialog = true;
    },
    editFare(){
      this.loading = true;
      createFares(this.formCreate)
        .then(data => {
          this.$message.success('操作成功!')
          this.editVisableDialog=false
          this.loading = false;
        }).catch(error=> {
          if(error.message.indexOf('timeout')!=-1){
            this.$message.warning('生成需要一定时间，请稍后查看!')
            this.editVisableDialog=false
            this.loading = false;
          }else{
            this.$message.error('操作失败!')
            this.loading = false;
          }
        });
    }
  }
}
</script>
<style lang="scss">
.bottom-menu-income {
  margin-top: -30px;
  text-align: center;
}
</style>
